﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraPivotGrid;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Sistem
{
    public partial class Tela : XForm
    {

        RepositoryItemComboBox riLine
        {
            get
            {
                RepositoryItemComboBox ri = new RepositoryItemComboBox();
                ri.TextEditStyle = TextEditStyles.DisableTextEditor;
                ri.Items.AddRange(new object[] { "ERKEK", "BAYAN", "ÇOCUK", "ERKEK ÇOCUK", "KIZ ÇOCUK", " " });
                return ri;
            }
        }

        RepositoryItemComboBox riAlternatif
        {
            get
            {
                RepositoryItemComboBox ri = new RepositoryItemComboBox();
                ri.TextEditStyle = TextEditStyles.DisableTextEditor;
                ri.Items.AddRange(new object[] { "KULLANILABİLİR", "KULLANILAMAZ", " " });
                return ri;
            }
        }

        public Tela()
        {
            InitializeComponent();
            txtInfo.Caption = "Tela";

            TAM.TelaMuadilTableAdapter = new ConfExpert.Datasets.MyDataTableAdapters.TelaMuadilTableAdapter();
            TAM.TelaMatrixTableAdapter = new ConfExpert.Datasets.MyDataTableAdapters.TelaMatrixTableAdapter();
        }

        BindingSource bsTelaMuadil, bsTelaMatrix;
        private void Tela_Load(object sender, EventArgs e)
        {
            
            TAM.TelaMuadilTableAdapter.Fill(ds.TelaMuadil);
            TAM.TelaMatrixTableAdapter.Fill(ds.TelaMatrix);

            bsTelaMuadil = new BindingSource(ds, "TelaMuadil");
            bsTelaMatrix = new BindingSource(ds, "TelaMatrix");

            InitGridView(gvTelaMatrix, bsTelaMatrix);
            
            gvTelaMatrix.Columns["Line"].ColumnEdit = riLine;
            gvTelaMatrix.Columns["Alternatif"].ColumnEdit = riAlternatif;

            gvTelaMatrix.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gvTelaMatrix.BestFitMaxRowCount = 50;
            gvTelaMatrix.BestFitColumns();
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            InitPG();
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Save();
        }

        //Skin currentSkin = CommonSkins.GetSkin(defaultLookAndFeel1.LookAndFeel);
        //Color c = currentSkin.TranslateColor(SystemColors.Control);

        void InitPG()
        {
            pivotGridControl1.DataSource = bsTelaMuadil;
            pivotGridControl1.BeginUpdate();
            AddNewField("Vendor", PivotArea.FilterArea);
            AddNewField("AltNr", PivotArea.RowArea);
            AddNewField("Vendor", PivotArea.ColumnArea);
            AddNewField("Gramaj", PivotArea.DataArea).Width = 40;
            AddNewField("Kod", PivotArea.DataArea);

            //pivot.Fields["Gramaj"].Appearance.Cell.BackColor = CommonSkins.GetSkin(DevExpress.LookAndFeel.UserLookAndFeel.Default).TranslateColor(SystemColors.Control);
            //pivot.Fields["Gramaj"].Appearance.Cell.ForeColor = Color.Indigo;
            //pivot.Fields["Vendor"].Appearance.Cell.BorderColor = Color.BlueViolet;

            pivotGridControl1.OptionsBehavior.ApplyBestFitOnFieldDragging = true;
            pivotGridControl1.OptionsView.ShowRowGrandTotals = false;
            pivotGridControl1.OptionsView.ShowColumnGrandTotals = false;
            pivotGridControl1.EndUpdate();

            pivotGridControl1.EditValueChanged += new DevExpress.XtraPivotGrid.EditValueChangedEventHandler(pivotGridControl_EditorValueChanged);
            pivotGridControl1.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pgc_FieldValueDisplayText);
            pivotGridControl1.CustomDrawCell+=new PivotCustomDrawCellEventHandler(pivotGridControl1_CustomDrawCell);
        }

        private void pivotGridControl1_CustomDrawCell(object sender, DevExpress.XtraPivotGrid.PivotCustomDrawCellEventArgs e) {
            if(e.ColumnValueType == PivotGridValueType.Value && e.DataField.FieldName == "Gramaj") {
                Rectangle innerRect = Rectangle.Inflate(e.Bounds, -5, -5);
                e.GraphicsCache.DrawString(e.DisplayText, e.Appearance.Font, e.Appearance.GetForeBrush(e.GraphicsCache), innerRect, e.Appearance.GetStringFormat());
                e.Graphics.DrawLine(new Pen(Color.Black, 1), e.Bounds.Right-2, e.Bounds.Y, e.Bounds.Right-2, e.Bounds.Bottom);
                e.Handled = true;
            }
        }        

        void pgc_FieldValueDisplayText(object sender, PivotFieldDisplayTextEventArgs e)
        {
            if (e.Value != null && e.Field != null && e.Field.FieldEdit != null)
                e.DisplayText = (e.Field.FieldEdit as RepositoryItemGridLookUpEdit).GetDisplayTextByKeyValue(e.Value).ToString();
        }

        private void pivotGridControl_EditorValueChanged(object sender, EditValueChangedEventArgs e)
        {
            try
            {
                PivotDrillDownDataSource ds = e.CreateDrillDownDataSource();
                string val = e.Editor.EditValue.ToString();
                if (e.DataField.FieldName == "Gramaj")
                {
                    int intVal = int.Parse(val);
                    for (int i = 0; i < ds.RowCount; i++)
                        ds[i][e.DataField.FieldName] = intVal;
                }
                else if (e.DataField.FieldName == "Kod")
                {
                    for (int i = 0; i < ds.RowCount; i++)
                        ds[i][e.DataField.FieldName] = val;
                }

            }
            catch (Exception Ex)
            {                
                throw Ex;
            }
        }

        private PivotGridField AddNewField(string FieldName, PivotArea pa)
        {
            PivotGridField fd = new PivotGridField();
            fd.FieldName = fd.Caption = FieldName;
            if (pa == PivotArea.DataArea)
            {
                fd.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Max;
                fd.FieldEdit = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
            }
            pivotGridControl1.Fields.Add(fd);
            fd.SetAreaPosition(pa, 0);
            return fd;
        }
    }
}